package ray.mgocc;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RuleScore extends ArrayList<RuleScore.Item>{
    private final static Log log = LogFactory.getLog(RuleScore.class);
    private Iterator pointer;
    private Item target;

    public RuleScore(){
        init();
    }
    private void init(){
        add(new Item("HS"));
        add(new Item("LOCK ON"));
        add(new Item("OTHER"));
        add(new Item("ALL"));
        pointer = iterator();
        if(pointer.hasNext()){
            target = (Item)pointer.next();
        }
    }
    protected Item get(String label){
        Item item = null;
        for(Item sample:this){
            if(sample.getName().equals(label)){
                item = sample;
            }
        }
        return item;
    }
    public Item getHeadShot(){
        return get("HS");
    }
    public Item getLockOn(){
        return get("LockOn");
    }
    public Item getOther(){
        return get("Other");
    }
    public Item getAll(){
        return get("ALL");
    }
    public boolean push(int value){
        boolean empty = true;
        boolean targetEmpty = target.push(value);
        if(!targetEmpty){
            if(pointer.hasNext()){
                target = (Item)pointer.next();
            }else{
                empty = false;
            }
        }
        return empty;
    }
    public String toString(){
        return super.toString();
    }
    public class Item extends HashMap<String,Integer>{
        private String name;
        private List<String> labels;
        private Iterator<String> pointer;

        Item(String name){
            this.name = name;
            init();
        }
        private void init(){
            labels = new ArrayList<String>();
            labels.add("KILLS");
            labels.add("DEATHS");
            labels.add("STUNS");
            labels.add("STUNS RECIEVED");
            pointer = labels.iterator();
        }
        public String getName(){
            return this.name;
        }
        /**
         * @return まだ入るかどうか。true:まだ入る。
         */
        public boolean push(int value){
            if(pointer.hasNext()){
                String label = pointer.next();
                put(label,new Integer(value));
            }
            return pointer.hasNext();
        }
        protected int get(String label){
            Integer value = super.get(label);
            if(null==value){
                value = new Integer(0);
            }
            return value.intValue();
        }
        public int getKills(){
            return get(labels.get(0));
        }
        public int getDeaths(){
            return get(labels.get(1));
        }
        public int getStuns(){
            return get(labels.get(2));
        }
        public int getStunsRecieved(){
            return get(labels.get(3));
        }
        public String toString(){
            StringBuilder b = new StringBuilder();
            for(String label:labels){
                if(0<b.length()){
                    b.append(",");
                }
                b.append(label).append("=").append(get(label));
            }
            b.insert(0,"[");
            b.append("]");
            b.insert(0,getName());
            return b.toString();
        }
    }
}
